import resources.RegistNetPropertyBundle;
import registnet.dao.CoursePeer;
registnet.dao.SectionPeer sectionPeer = new registnet.dao.SectionPeer();
registnet.dao.SectionConfigurationPeer sectionConfigurationPeer = new registnet.dao.SectionConfigurationPeer();
registnet.dao.Section section;

List data = new Vector();
List resultSet = new Vector();

RegistNetPropertyBundle bundle = RegistNetPropertyBundle.getInstance("/registnet/resources/registnet");
String _add = bundle.getProperty("registnet.module.data_management.actions.menu.add");
String _edit = bundle.getProperty("registnet.module.data_management.actions.menu.edit");
String _delete = bundle.getProperty("registnet.module.data_management.actions.menu.delete");
String _import = bundle.getProperty("registnet.module.data_management.actions.menu.import");
String _search = bundle.getProperty("registnet.module.data_management.actions.menu.search");
String _successful = bundle.getProperty("registnet.successful");


String _proceed = bundle.getProperty("registnet.messages.proceed");
String _check = bundle.getProperty("registnet.messages.check");
String _successful = bundle.getProperty("registnet.messages.successful");
String _failed = bundle.getProperty("registnet.messages.failed");
String _select_one = bundle.getProperty("registnet.messages.select_one");
String _selected = bundle.getProperty("registnet.messages.selected");


String _user_type = session.getAttribute("_user_type");
String _office = session.getAttribute("_office");
String _lec = "Lecture";
String _lab = "Laboratory";

String _parentCourse;
String _instance = "Section";
String sectionType;

boolean isShowingInfo;
boolean isSearching;
boolean hasLab=false, hasRecit=false;
boolean isShowingInfo;
boolean isLaboratorySection;
boolean isSearching;

int sectionConfigurationId;

ArrayList alc = new ArrayList();
Map mu = new HashMap();

SimpleTreeNode root = new SimpleTreeNode("ROOT", alc);

public class MyTreeitemRenderer implements TreeitemRenderer {

	public void render(Treeitem item, Object data) throws Exception {
		
		String str = (String)data.getData();
	
		Treerow tr = null;
		
		Treecell tc = new Treecell(str);
		if(item.getTreerow()==null){
			tr = new Treerow();
			tr.setParent(item);
			try{
				item.setId(str + "_config");
				item.setOpen(true);
			}catch(Exception e){}
		}else{
			tr = item.getTreerow(); 
			tr.getChildren().clear();
		}
		tc.setParent(tr);
	}
}

void showAll(){
}

private class SelectEventListener implements EventListener {
	
	public SelectEventListener (){
	}
			
	public void onEvent(Event event) throws UiException {
		showInfo();
	}
}	

private class CancelEventListener implements EventListener {
	public SubmitEventListener (){
	}
			
	public void onEvent(Event event) throws UiException {
		instance_tree.clearSelection();
		try{
			title.setValue("");
			contents.removeChild(contents.getFirstChild());
		} catch (Exception e) {}
	}
}

private class SearchEventListener implements EventListener {
	
	public SearchEventListener (){
	}
			
	public void onEvent(Event event) throws UiException {
		
		int isConfirmed = Messagebox.show("Do you want to use the Search Result Viewer?", "", Messagebox.YES | Messagebox.NO, Messagebox.QUESTION);
		if (isConfirmed == Messagebox.YES){
			resultSet = sectionPeer.searchSection(section_code.value, section_name.value);
			showSearchResultViewer();
		}
		else
			returnSearchResults();
			
		try{
			title.setValue("");
			contents.removeChild(contents.getFirstChild());
		} catch (Exception e) {}
	}
}


public void retrieveObjects(){
	alc.clear();
	data.clear();
	String[] info = macro_window.getParent().getTabbox().getSelectedTab().getLabel().split("_");
	sectionConfigurationId = (new registnet.dao.SectionConfigurationPeer()).getSectionConfigurationId((new registnet.dao.CoursePeer()).getCourseId(info[0]), info[1]);
	
	List lectures = (new registnet.dao.SectionPeer()).getLectureSections(sectionConfigurationId);
	for(int i=0; i<lectures.size(); i++){
		List labs = (new registnet.dao.SectionPeer()).getLaboratory(sectionConfigurationId, lectures.get(i).getSectionName());
		ArrayList alu = new ArrayList();
		if(labs != null){
			for(int j=0; j<labs.size(); j++){
				data.add(labs.get(j));
				int labStudentCount = 0;
				SimpleTreeNode stnSection = new SimpleTreeNode(labs.get(j).getSectionName(), new ArrayList());
				alu.add(stnSection);
			}
		}
		mu.put(lectures.get(i).getSectionName(), alu);
		SimpleTreeNode stnCourse = new SimpleTreeNode(lectures.get(i).getSectionName(), alu);
		alc.add(stnCourse);
		
	}

	SimpleTreeNode root = new SimpleTreeNode("ROOT", alc);
	SimpleTreeModel stm = new SimpleTreeModel(root);
	instance_tree.setModel(stm);
	instance_tree.setTreeitemRenderer(new MyTreeitemRenderer());
}

void returnSearchResults(){

	data.clear();
	
	resultSet = sectionPeer.searchSection(course_code.value, section_name.value);
	
	ListIterator li = resultSet.listIterator();
	while(li.hasNext()){
		data.add(((registnet.dao.Section)li.next()).getCourseCode());
	}

	instance_list.setModel(new ListModelList(data));
	instance_list.setItemRenderer(new MyItemRenderer());
}

void showInfo(){
	isShowingInfo = true;
	isSearching = false;
	HashMap hm = resources.Constants.MAP_SEMESTER_VALUE;
	if (contents.getFirstChild() != null){
		contents.removeChild(contents.getFirstChild());
	}
	try{
		if (instance_tree.getSelectedCount() == 1){
			section = sectionPeer.getSection(sectionConfigurationId,instance_tree.getSelectedItem().getLabel());
			title.setValue("CMSC 1");
			Component show_unit_form = Executions.createComponents("/WEB-INF/macros/data_management/section_form.zul", contents, null);
			show_unit_form.getFellow("section_name_l").setValue(section.getSectionName());
			show_unit_form.getFellow("section_name_l").setVisible(true);
			show_unit_form.getFellow("number_of_students_l").setValue(section.getStudentCount()+"");
			show_unit_form.getFellow("lecture_day_l").setValue(section.getLectureDay());
			show_unit_form.getFellow("lecture_room_l").setValue(section.getLectureRoom());
			show_unit_form.getFellow("lecture_time_l").setValue(section.getLectureTime());
			show_unit_form.getFellow("lecture_instructor_l").setValue(section.getLectureInstructor());
			if(section.getIsTeamTeach()==1)
				show_unit_form.getFellow("team_teach_l").setValue("Yes");
			else
				show_unit_form.getFellow("team_teach_l").setValue("No");
			
			//int lab = new registnet.dao.CoursePeer().getCourse(sectionConfigurationPeer.getSectionConfiguration(sectionConfigurationId).getCourseId()).getHasLaboratory();
			
			String labDay = section.getLaboratoryDay();
			String labRoom = section.getLaboratoryRoom();
			String labTime = section.getLaboratoryTime();
			String labInstructor = section.getLaboratoryInstructor();
			
			show_unit_form.getFellow("save_btn").detach();
			show_unit_form.getFellow("cancel_btn").detach();
			if(labDay!=null || labRoom!=null || labTime!=null || labInstructor!=null){
				hasLab = true;
				isLaboratorySection = true;
				show_unit_form.getFellow("labDay").setVisible(true);
				show_unit_form.getFellow("labRoom").setVisible(true);
				show_unit_form.getFellow("labTime").setVisible(true);
				show_unit_form.getFellow("labIns").setVisible(true);
				
				show_unit_form.getFellow("laboratory_day_l").setValue(labDay);
				show_unit_form.getFellow("laboratory_room_l").setValue(labRoom);
				show_unit_form.getFellow("laboratory_time_l").setValue(labTime);
				show_unit_form.getFellow("laboratory_instructor_l").setValue(labInstructor);
			}
			else{
				isLaboratorySection = false;
			}
			
			/*
			int recit = new registnet.dao.CoursePeer().getCourse(section.getCourseCode()).getHasRecitation();
			if(recit==1){
				hasRecit=true;
				show_unit_form.getFellow("recitDay").setVisible(true);
				show_unit_form.getFellow("recitRoom").setVisible(true);
				show_unit_form.getFellow("recitTime").setVisible(true);
				show_unit_form.getFellow("recitIns").setVisible(true);
				
				show_unit_form.getFellow("recitation_day_l").setValue(section.getRecitationDay());
				show_unit_form.getFellow("recitation_room_l").setValue(section.getRecitationRoom());
				show_unit_form.getFellow("recitation_time_l").setValue(section.getRecitationTime());
				show_unit_form.getFellow("recitation_instructor_l").setValue(section.getRecitationInstructor());
			}
			*/
		}
		else if (instance_tree.getSelectedCount() > 1)
			title.setValue("" + instance_tree.getSelectedCount() + " " + _instance + " " + _selected);
		else
			title.setValue("");
	}catch (Exception e) { title.setValue(""); }
}


void showForm(String action){

	isShowingInfo = false;
	isSearching = false;

	try{
		contents.removeChild(contents.getFirstChild());
	} catch (Exception e) {}

	
	if (action.equals(_edit)){
		if(instance_tree.getSelectedCount() == 1){
			HashMap hm = resources.Constants.MAP_SEMESTER_VALUE;
			Component show_unit_form = Executions.createComponents("/WEB-INF/macros/data_management/section_form.zul", contents, null);
			section = sectionPeer.getSection(sectionConfigurationId,instance_tree.getSelectedItem().getLabel());
			title.setValue("CMSC 1");
			show_unit_form.setParent(contents);
			show_unit_form.getFellow("section_name_l").setValue(section.getSectionName());
			show_unit_form.getFellow("section_name_l").setVisible(true);
			show_unit_form.getFellow("number_of_students").setValue(section.getStudentCount());
			if(!isLaboratorySection){
				show_unit_form.getFellow("lecture_day").setValue(section.getLectureDay());
				show_unit_form.getFellow("lecture_room").setValue(section.getLectureRoom());
				
				
				
				if( section.getLectureTime() != null){
					String[] time = section.getLectureTime().split("-");
					String[] start_time = time[0].split(":");
					String[] end_time = time[1].split(":");
					
						
					if(time!=null){
							show_unit_form.getFellow("start_lecture_hrs").setValue(start_time[0]);
							try{
								show_unit_form.getFellow("start_lecture_mins").setValue(start_time[1]);
							}catch(Exception e){
								show_unit_form.getFellow("start_lecture_mins").setValue("00");
							}
							
							show_unit_form.getFellow("end_lecture_hrs").setValue(end_time[0]);
							try{
								show_unit_form.getFellow("end_lecture_mins").setValue(end_time[1]);
							}catch(Exception e){
								show_unit_form.getFellow("end_lecture_mins").setValue("00");
							}
					}
				}
				
				show_unit_form.getFellow("lecture_instructor").setValue(section.getLectureInstructor());
			}
			else{
				show_unit_form.getFellow("lecDay").setVisible(false);
				show_unit_form.getFellow("lecTime").setVisible(false);
				show_unit_form.getFellow("lecIns").setVisible(false);
				show_unit_form.getFellow("lecRoom").setVisible(false);
			}
			if(section.getIsTeamTeach()==1)
				show_unit_form.getFellow("team_teach").setValue("Yes");
			else
				show_unit_form.getFellow("team_teach").setValue("No");
			
			//int lab = new registnet.dao.CoursePeer().getCourse(section.getCourseCode()).getHasLaboratory();
			show_unit_form.getFellow("section_name").setVisible(false);	
			if(isLaboratorySection){
				hasLab = true;
				show_unit_form.getFellow("labDay").setVisible(true);
				show_unit_form.getFellow("labRoom").setVisible(true);
				show_unit_form.getFellow("labTime").setVisible(true);
				show_unit_form.getFellow("labIns").setVisible(true);
				//show_unit_form.getFellow("section_name_h").setVisible(true);
				
				
				show_unit_form.getFellow("laboratory_day").setValue(section.getLaboratoryDay());
				show_unit_form.getFellow("laboratory_room").setValue(section.getLaboratoryRoom());
				/*
				List lectureSections = sectionPeer.getLectureSections(section.getCourseId());
				String[] data = new String[lectureSections.size()];
				
				for(int i=0; i<data.length; i++){
					data[i] = lectureSections.get(i).getSectionName();
				}
				ListModel strset = new ListModelList(data);
				show_unit_form.getFellow("section_name_b").setModel(strset);
				show_unit_form.getFellow("section_name_b").setItemRenderer(new FieldItemRenderer());
				show_unit_form.getFellow("section_name_b").setSelectedIndex(0);
				
				String labSection = section.getSectionName().split("-")[1]; 
				show_unit_form.getFellow("lab_section").setValue(labSection);
				*/
				
				
				
				if( section.getLaboratoryTime() != null){
					String[] time = section.getLaboratoryTime().split("-");
					String[] start_time = time[0].split(":");
					String[] end_time = time[1].split(":");
					
						
					if(time!=null){
							show_unit_form.getFellow("start_lab_hrs").setValue(start_time[0]);
							try{
								show_unit_form.getFellow("start_lab_mins").setValue(start_time[1]);
							}catch(Exception e){
								show_unit_form.getFellow("start_lab_mins").setValue("00");
							}
							
							show_unit_form.getFellow("end_lab_hrs").setValue(end_time[0]);
							try{
								show_unit_form.getFellow("end_lab_mins").setValue(end_time[1]);
							}catch(Exception e){
								show_unit_form.getFellow("end_lab_mins").setValue("00");
							}
					}
				}
				
				show_unit_form.getFellow("laboratory_instructor").setValue(section.getLaboratoryInstructor());
			}
			else
				hasLab = false;
			/*
			int recit = new registnet.dao.CoursePeer().getCourse(section.getCourseId()).getHasRecitation();
			if(recit==1){
				show_unit_form.getFellow("recitDay").setVisible(true);
				show_unit_form.getFellow("recitRoom").setVisible(true);
				show_unit_form.getFellow("recitTime").setVisible(true);
				show_unit_form.getFellow("recitIns").setVisible(true);
				
				show_unit_form.getFellow("recitation_day").setValue(section.getRecitationDay());
				show_unit_form.getFellow("recitation_room").setValue(section.getRecitationRoom());
				
				if( section.getRecitationTime() != null){
					String[] time = section.getRecitationTime().split("-");
					String[] start_time = time[0].split(":");
					String[] end_time = time[1].split(":");
					
						
					if(time!=null){
							show_unit_form.getFellow("start_recit_hrs").setValue(start_time[0]);
							try{
								show_unit_form.getFellow("start_recit_mins").setValue(start_time[1]);
							}catch(Exception e){
								show_unit_form.getFellow("start_recit_mins").setValue("00");
							}
							
							show_unit_form.getFellow("end_recit_hrs").setValue(end_time[0]);
							try{
								show_unit_form.getFellow("end_recit_mins").setValue(end_time[1]);
							}catch(Exception e){
								show_unit_form.getFellow("end_recit_mins").setValue("00");
							}
					}
				}
				
				show_unit_form.getFellow("recitation_instructor").setValue(section.getRecitationInstructor());
			}*/
			
			show_unit_form.getFellow("save_btn").addEventListener("onClick", new SubmitEventListener(action));
			show_unit_form.getFellow("cancel_btn").addEventListener("onClick", new CancelEventListener());
		}
		else{
			alert(_select_one + " " + _instance);
		}
	}
	
	else if (action.equals(_add)){
				
				if(sectionType.equals(_lec)){
					isLaboratorySection=false;
					Component edit_unit_form = Executions.createComponents("/WEB-INF/macros/data_management/section_form.zul", contents, null);
					title.setValue(_add + " " + _instance);
					edit_unit_form.getFellow("save_btn").addEventListener("onClick", new SubmitEventListener(action));
					edit_unit_form.getFellow("cancel_btn").addEventListener("onClick", new CancelEventListener());
					
				}
				else{
					isLaboratorySection=true;
					List lectureSections = sectionPeer.getLectureSections(sectionConfigurationId);
					int size = lectureSections.size();
					String[] data = new String[size];
					
					if(size!=0){
						Component edit_unit_form = Executions.createComponents("/WEB-INF/macros/data_management/section_form.zul", contents, null);
						title.setValue(_add + " " + _instance);
						
						edit_unit_form.setParent(contents);
						edit_unit_form.getFellow("save_btn").addEventListener("onClick", new SubmitEventListener(action));
						edit_unit_form.getFellow("cancel_btn").addEventListener("onClick", new CancelEventListener());
						
						edit_unit_form.getFellow("section_name").setVisible(false);
						edit_unit_form.getFellow("lecDay").setVisible(false);
						edit_unit_form.getFellow("lecRoom").setVisible(false);
						edit_unit_form.getFellow("lecTime").setVisible(false);
						edit_unit_form.getFellow("lecIns").setVisible(false);
						
						edit_unit_form.getFellow("section_name_h").setVisible(true);					
						edit_unit_form.getFellow("labDay").setVisible(true);
						edit_unit_form.getFellow("labRoom").setVisible(true);
						edit_unit_form.getFellow("labTime").setVisible(true);
						edit_unit_form.getFellow("labIns").setVisible(true);
						
						
						for(int i=0; i<data.length; i++){
							data[i] = lectureSections.get(i).getSectionName();
						}
						ListModel strset = new ListModelList(data);
						edit_unit_form.getFellow("section_name_b").setModel(strset);
						edit_unit_form.getFellow("section_name_b").setItemRenderer(new FieldItemRenderer());
						edit_unit_form.getFellow("section_name_b").setSelectedIndex(0);
					}
					else
						alert("Please make a lecture section first.");	
				}
	}
	
	
}

setSectionType(String type){
	sectionType=type;
	modal_win.detach();
}


boolean checkCourse(String courseCode){
	registnet.dao.Course course = (new registnet.dao.CoursePeer()).getCourse(courseCode);
	if(course==null){
		return false;
	}
	else{
		int lab = course.getHasLaboratory();
			if(lab==1)
				hasLab = true;
			else
				hasLab = false;
		return true;
	}
}


void delete(){
	if(instance_list.getSelectedCount() != 0){
		int isConfirmed = Messagebox.show(_proceed, _delete + " " + _instance, Messagebox.YES | Messagebox.NO, Messagebox.QUESTION);
		if (isConfirmed == Messagebox.YES){
			boolean isError = false;
			int errorCount = 0;
			Iterator i = instance_list.getSelectedItems().iterator();
			instance_list.renderAll();
			while(i.hasNext()){
				String string = i.next().getLabel();
				int temp = sectionPeer.getSectionId(string);
				if (sectionPeer.deleteSection(temp))
					data.remove(string);
				else{
					isError = true;
					errorCount++;
				}
			}
			instance_list.setModel(new ListModelList(data));
			if (isError){ 
				alert(_delete + " " + _failed + ": " + errorCount + " " + _instance);
				title.setValue(_delete + " " + _instance + " " + _failed);
			}
			
			else{
				title.setValue(_delete + " " + _instance + " " + _successful);
				if (contents.getFirstChild() != null){
					contents.removeChild(contents.getFirstChild());
				}
			}
		}			
	}
}

void importData(){
	List temp = new Vector();
	org.zkoss.util.media.Media media = Fileupload.get();
	if(media != null)
		temp = sectionPeer.importSections(media.getStringData());
	if(temp != null)
		data.addAll(temp);
	instance_list.clearSelection();
	instance_list.setModel(new ListModelList(data));
	instance_list.setItemRenderer(new MyItemRenderer());
	title.setValue(_import + " " + _instance + " " + _successful);
}

void createNewSection(){
	
	boolean hasError=false;
		
		if(team_teach.getValue().equals(""))
			hasError = true;
		
		String sectionName;
		String lectureDay, lectureRoom, lectureInstructor;
		if(isLaboratorySection)
				sectionName = section_name_b.getSelectedItem().getLabel() + "-" + lab_section.value;
		else{
			if(section_name.getValue().equals(""))
				hasError = true;
			else
				sectionName = section_name.getValue();
		}
		
		String lectureTime = new String();
		String recitTime = new String();
		String labTime = null;
		String labDay = null;
		String labRoom = null;
		String labInstructor = null;
		
		if(!isLaboratorySection){
			if(!start_lecture_hrs.getValue().equals("") && !start_lecture_mins.getValue().equals("") && !end_lecture_hrs.getValue().equals("") && !end_lecture_mins.getValue().equals("")){
				if(start_lecture_mins.getValue().equals("00"))
					lectureTime = start_lecture_hrs.getValue();
				else
					lectureTime = start_lecture_hrs.getValue()+":"+start_lecture_mins.getValue();
				
				if(end_lecture_mins.getValue().equals("00"))
					lectureTime = lectureTime+"-"+ end_lecture_hrs.getValue();
				else
					lectureTime = lectureTime+"-"+end_lecture_hrs.getValue()+":"+end_lecture_mins.getValue();
			}
			lectureDay = lecture_day.value;
			lectureRoom = lecture_room.value;
			lectureInstructor = lecture_instructor.value;
			
		}
		else{
			section = sectionPeer.getSection(sectionConfigurationId, section_name_b.getSelectedItem().getLabel());
			lectureDay = section.getLectureDay();
			lectureTime = section.getLectureTime();
			lectureRoom = section.getLectureRoom();
			lectureInstructor = section.getLectureInstructor();
		}
			
		if(isLaboratorySection){
			if(!start_lab_hrs.getValue().equals("") && !start_lab_mins.getValue().equals("") && !end_lab_hrs.getValue().equals("") && !end_lab_mins.getValue().equals("")){
				if(start_lab_mins.getValue().equals("00"))
					labTime = start_lab_hrs.getValue();
				else
					labTime = start_lab_hrs.getValue()+":"+start_lab_mins.getValue();
				
				if(end_lab_mins.getValue().equals("00"))
					labTime = labTime+"-"+ end_lab_hrs.getValue();
				else
					labTime = labTime+"-"+end_lab_hrs.getValue()+":"+end_lab_mins.getValue();
			}
			labDay = laboratory_day.value;
			labRoom = laboratory_room.value;
			labInstructor = laboratory_instructor.value;
		}
		/*
		if(hasRecit){
			if(!recit_hrs.getValue().equals("") && !recit_mins.getValue().equals("")){
				if(recit_mins.getValue().equals("00"))
					recitTime = recit_hrs.getValue();
				else
					recitTime = recit_hrs.getValue()+":"+recit_mins.getValue();
			}
		}
		*/
		
		
		if(!hasError){
			HashMap hm = resources.Constants.MAP_SEMESTER_KEY;
			title.setValue(_add + " " + _instance + " " + _successful);
			if(sectionPeer.createNewSection(sectionConfigurationId, sectionName, labDay, lectureDay, recitation_day.value, labRoom, lectureRoom, recitation_room.value, labTime, lectureTime, recitTime, labInstructor, lectureInstructor, recitation_instructor.value, Integer.parseInt(team_teach.getSelectedItem().getValue()), number_of_students.value) != null){
				retrieveObjects();
				String lec = sectionName.split("-")[0];
				data.add(0, lec);
				//if(sectionName.contains("-"))
					//macro_win.getFellow(lec + "_course").setOpen(true);
				try{
					contents.removeChild(contents.getFirstChild());
				} catch (Exception e) { }
			}
			else
				alert("Please check info supplied");
				
		}
}

void editSection(){
	boolean hasError=false;
		/*String sectionName;
		if(isLaboratorySection)
				sectionName = section_name_b.getSelectedItem().getLabel() + "-" + lab_section.value;
		else{
			if(section_name.getValue().equals(""))
				hasError = true;
				sectionName = section_name.getValue();
		}*/
		
		String lectureDay = new String();
		String lectureInstructor = new String();
		String lectureTime = null;
		String lectureRoom = new String();
		String labTime = null;
		String recitTime = new String();
		String labDay = null;
		String labRoom = null;
		String labInstructor = null;
		
		
		
		
			
		
		if(hasRecit){
			if(!recit_hrs.getValue().equals("") && !recit_mins.getValue().equals("")){
				if(recit_mins.getValue().equals("00"))
					recitTime = recit_hrs.getValue();
				else
					recitTime = recit_hrs.getValue()+":"+recit_mins.getValue();
			}
		}
		
		if(isLaboratorySection){
			section = sectionPeer.getSection(sectionConfigurationId, section_name_l.value.split("-")[0]);
			lectureDay = section.getLectureDay();
			lectureTime = section.getLectureTime();
			lectureRoom = section.getLectureRoom();
			lectureInstructor = section.getLectureInstructor();
			labDay = laboratory_day.value;
			labRoom = laboratory_room.value;
			labInstructor = laboratory_instructor.value;
			if(!start_lab_hrs.getValue().equals("") && !start_lab_mins.getValue().equals("") && !end_lab_hrs.getValue().equals("") && !end_lab_mins.getValue().equals("")){
				if(start_lab_mins.getValue().equals("00"))
					labTime = start_lab_hrs.getValue();
				else
					labTime = start_lab_hrs.getValue()+":"+start_lab_mins.getValue();
				
				if(end_lab_mins.getValue().equals("00"))
					labTime = labTime+"-"+ end_lab_hrs.getValue();
				else
					labTime = labTime+"-"+end_lab_hrs.getValue()+":"+end_lab_mins.getValue();
			}
		}
		else{
			if(!start_lecture_hrs.getValue().equals("") && !start_lecture_mins.getValue().equals("") && !end_lecture_hrs.getValue().equals("") && !end_lecture_mins.getValue().equals("")){
				if(start_lecture_mins.getValue().equals("00"))
					lectureTime = start_lecture_hrs.getValue();
				else
					lectureTime = start_lecture_hrs.getValue()+":"+start_lecture_mins.getValue();
				
				if(end_lecture_mins.getValue().equals("00"))
					lectureTime = lectureTime+"-"+ end_lecture_hrs.getValue();
				else
					lectureTime = lectureTime+"-"+end_lecture_hrs.getValue()+":"+end_lecture_mins.getValue();
			}
			lectureDay = lecture_day.value;
			lectureRoom = lecture_room.value;
			lectureInstructor = lecture_instructor.value;
		}
	
	if (!hasError){
		try{
			sectionPeer.updateSection(sectionPeer.getSectionId(sectionConfigurationId,instance_tree.getSelectedItem().getLabel() ), sectionConfigurationId, section_name_l.value, labDay, lectureDay, recitation_day.value, labRoom, lectureRoom, recitation_room.value, labTime, lectureTime, recitTime, labInstructor, lectureInstructor, recitation_instructor.value, Integer.parseInt(team_teach.getSelectedItem().getValue()), number_of_students.value);
			List labSections = sectionPeer.getLaboratory(sectionConfigurationId, section_name_l.value);
			for(int i=0; i<labSections.size(); i++){
				sectionPeer.updateLabSection(labSections.get(i).getSectionId(), labSections.get(i).getSectionConfigurationId(), labSections.get(i).getSectionName(),lecture_day.value,  lecture_room.value, lectureTime, lecture_instructor.value);
			}
			retrieveObjects();
			//macro_win.getFellow(course_code_l.value + "_course").setOpen(true);
			title.setValue(_edit + " " + _instance + " " + _successful);
			try{
				contents.removeChild(contents.getFirstChild());
			} catch (Exception e) {}
		} catch (Exception e) { alert(_check); }
	}
	else{
		alert(_check);
	};
	
}

boolean isValid(){
	if (section_code.value != null && section_code.value.trim().length() == 0)
		return false;
	if (section_name.value != null && section_name.value.trim().length() == 0)
		return false;
	return true;
}

private class SubmitEventListener implements EventListener {
	String submitType;
	public SubmitEventListener (String submitType){
		this.submitType = submitType; 
	}
			
	public void onEvent(Event event) throws UiException {
		if(this.submitType.equals(_add))
			createNewSection();
		else if (this.submitType.equals(_edit))
			editSection();
	}
}

void filter(String string){

	ListIterator i = data.listIterator();
	List list = new java.util.Vector();
	while(i.hasNext()){
		String temp = i.next(); 
		if(temp.toUpperCase().startsWith(string.toUpperCase())){
			list.add(temp);
		}
	}
	instance_list.getModel().clear();
	instance_list.getModel().addAll(list);
}

void showSearchForm(){
	isSearching = true;
	isShowingInfo = false;

	try{
		contents.removeChild(contents.getFirstChild());
	} catch (Exception e) {}

	Component search_section_form = Executions.createComponents("/WEB-INF/macros/data_management/section_form.zul", contents, null);
	title.setValue("Search for " + _instance);
	search_section_form.getFellow("search_btn").addEventListener("onClick", new SearchEventListener());
	search_section_form.getFellow("cancel_btn").addEventListener("onClick", new CancelEventListener());
}

void showSearchResultViewer(){
	Tabbox stage = macro_win.getParent().getParent().getTabbox();
	Tab tab = new Tab("Search Result");
	Tabs tabs = stage.getTabs();
	tab.setClosable(true);
	tab.setParent(tabs);
	stage.setSelectedTab(tab);
		
	Tabpanel tabpanel = new Tabpanel();
	tabpanel.setSclass("background_light_blue");
	
	
	Component search_result_viewer = Executions.createComponents("/WEB-INF/macros/search_result_viewer.zul", tabpanel, null);
		
	Tabpanels tabpanels = stage.getTabpanels();
	tabpanel.setParent(tabpanels);
	stage.setSelectedPanel(tabpanel);
}

private class FieldItemRenderer implements ListitemRenderer {
	public void render(Listitem item, java.lang.Object data) {
		new Listcell(data).setParent(item);
	}
}
			
private class DataItemRenderer implements ListitemRenderer {
	public void render(Listitem item, java.lang.Object data) {
		for(int i=0; i<data.length; i++)
			new Listcell(data[i]).setParent(item);
	}
}
	
void retrieveFieldNames(){
	List data = new registnet.dao.Section().getFields();		  
	String[] listModel = new String[data.size()];
	for (int i=0; i<data.size(); i++)
		listModel[i] = data.get(i);
	ListModel strset = new ListModelList(listModel);
	field_list.setModel(strset);
	field_list.setItemRenderer(new FieldItemRenderer());
}


private class FieldEventListener implements EventListener {
	
	public FieldEventListener (){
	}

	public void onEvent(Event event) throws UiException {
		result_list.removeChild(result_list.getListhead());
		Listhead listhead = new Listhead();
		listhead.setParent(result_list);
		Object[] selected = field_list.getSelectedItems().toArray();
		for (int i=0; i<selected.length; i++){
			Listheader lh = new Listheader(field_list.getModel().get(selected[i].getIndex()));
			lh.setParent(listhead);
		}
		
		
		String[] temp = new String[resultSet.size()];
		int numRows = temp.length;
		String[][] listModel = new String[numRows][selected.length];
		for (int i=0; i<temp.length; i++){
			for (int j=0; j<selected.length; j++){
				if (field_list.getModel().get(selected[j].getIndex()).equals("Section Code"))
					listModel[i][j] = resultSet.get(i).getSectionCode();
				else if (field_list.getModel().get(selected[j].getIndex()).equals("Section Name"))
					listModel[i][j] = resultSet.get(i).getSectionName();
			}
		}
		ListModel strset = new ListModelList(listModel);
		result_list.setModel(strset);
		result_list.setItemRenderer(new DataItemRenderer());
		
	}
}

showGradeSheet(){
	courseId = (new registnet.dao.CoursePeer()).getCourseId(instance_tree.getSelectedItem().getParent().getLinkedTreerow().getParent().getLabel());
	sectionName = instance_tree.getSelectedItem().getLabel();
	String section = (new registnet.dao.CoursePeer()).getCourse(courseId).getCourseCode() + "-" + sectionName;
	String date;
	List studentIds = (new registnet.dao.GradePeer()).getREGDStudentBySection(courseId, sectionName);
	if(studentIds.size!=0){
		int acadYear = studentIds.get(0).getAcademicYear();
		String semester = resources.Constants.MAP_SEMESTER_VALUE.get(studentIds.get(0).getSemester());
		date = semester +" Semester " + acadYear + "-" + (acadYear+1);  
		Component gradesheet_form = Executions.createComponents("/WEB-INF/macros/manager_gradesheet.zul", null, null);
		//gradesheet_form.getFellow("sectionCourse").setValue(section);
		//gradesheet_form.getFellow("sectionDate").setValue(date);
		title.setValue("Grade Sheet of " + section + " " + date);
		gradesheet_form.setParent(contents);
		contents.removeChild(contents.getFirstChild());
	}
	else
		alert("Grade already submitted. To view the gradesheet again, provide the academic year and semester for the section in the more menu.");
	
}

showClassList(){
	courseId = (new registnet.dao.CoursePeer()).getCourseId(instance_tree.getSelectedItem().getParent().getLinkedTreerow().getParent().getLabel());
	sectionName = instance_tree.getSelectedItem().getLabel();
	String section = (new registnet.dao.CoursePeer()).getCourse(courseId).getCourseCode() + " " + sectionName;
	String date;
	List studentIds = (new registnet.dao.GradePeer()).getREGDStudentBySection(courseId, sectionName);
	if(studentIds.size!=0){
		
		int acadYear = studentIds.get(0).getAcademicYear();
		String semester = resources.Constants.MAP_SEMESTER_VALUE.get(studentIds.get(0).getSemester());
		date = semester +" Semester " + acadYear + "-" + (acadYear+1);  
		Component classlist_form = Executions.createComponents("/WEB-INF/macros/manager_classlist.zul", null, null);
		//classlist_form.getFellow("sectionCourse").setValue(section);
		//classlist_form.getFellow("sectionDate").setValue(date);
		title.setValue("Class List of " + section + " " + date);
		classlist_form.setParent(contents);
		
		contents.removeChild(contents.getFirstChild());
		
	}
	else
		alert("No students found.");	
}

